package com.yc.service;

import brave.Span;
import brave.Tracer;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author: ZhouJianLong
 * @Date: 2024/06/27/14:41
 * @Description:
 */
@GrpcService
@Slf4j
public class GreeterImpl extends GreeterGrpc.GreeterImplBase{
    @Autowired
    Tracer tracer;
    @Override
    public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {

        Span newSpan = this.tracer.nextSpan().name("calculateTax");
        try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(newSpan.start())) {
            // ...
            // You can tag a span
            newSpan.tag("taxValue", "111");
            // ...
            // You can log an event on a span
            newSpan.annotate("taxCalculated");
        }
        finally {
            // Once done remember to finish the span. This will allow collecting
            // the span to send it to Zipkin
            newSpan.finish();
        }
        HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build();
        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }
}
